#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.biz.util;

import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Currency;
import java.util.Locale;

/**
 * Created by well on 15/3/17.
 */
public class PriceUtil {

    /**
     * 12345 -- >123.45
     *
     * @param cent
     * @return
     */
    public static String formatCNY(Long cent) {
        if (cent == null) {
            return null;
        }
        DecimalFormat format = new DecimalFormat();
        format.setCurrency(Currency.getInstance(Locale.CHINA));
        return format.format(cent / 100.00);
    }

    public static String formatCNY(Object cent) {
        if (cent == null) {
            return null;
        }
        Long c = Long.valueOf(String.valueOf(cent));
        DecimalFormat format = new DecimalFormat();
        format.setCurrency(Currency.getInstance(Locale.CHINA));
        return format.format(c / 100.00);
    }

    public static String format(Long cent) {
        if (cent == null) {
            return null;
        }
        DecimalFormat format = new DecimalFormat();
        format.setCurrency(Currency.getInstance(Locale.CHINA));
        return format.format(cent / 100.00);
    }

    public static String formatWan(Long cent) {
        if (cent == null) {
            return null;
        }
        BigDecimal bigDecimal = new BigDecimal(cent).divide(new BigDecimal(1000000));
        return bigDecimal.toString();
    }

    /**
     * 1234567800L  -> 123,456.7
     * 123000000L  -> 12,300
     *
     * @param cent
     * @return
     */
    public static String formatCNYW(Long cent) {
        if (cent == null) {
            return null;
        }
        DecimalFormat format = new DecimalFormat();
        format.setCurrency(Currency.getInstance(Locale.CHINA));
        format.setMinimumFractionDigits(0);
        format.setMaximumFractionDigits(2);
        format.setRoundingMode(RoundingMode.DOWN);
        format.setGroupingUsed(true);
        return format.format(cent / 1000000.00);
    }

    /**
     * @param price
     * @return
     */
    public static Long convertPrice(String price) {
        if (StringUtils.isEmpty(price)){
            return null;
        }
        BigDecimal bigDecimal = new BigDecimal(price);
        return bigDecimal.multiply(new BigDecimal(1000000)).longValue();
    }


    /**
     * 元转换进入数据库
     * @param price
     * @return
     */
    public static Long convertPriceYuan(String price) {
        if (StringUtils.isEmpty(price)){
            return null;
        }
        BigDecimal bigDecimal = new BigDecimal(price);
        return bigDecimal.multiply(new BigDecimal(100)).longValue();
    }

    public static long parseCentFromW(String w) {
        if (w == null) {
            return 0;
        }
        DecimalFormat format = new DecimalFormat();
        format.setCurrency(Currency.getInstance(Locale.CHINA));
        try {
            return (format.parse(w)).longValue() * 1000000;
        } catch (Exception e) {

        }
        return -1;
    }

    public static String formatYuan(Long cent, boolean groupingUsed, int maximumFractionDigits) {
        if (cent == null) {
            return null;
        }
        DecimalFormat format = new DecimalFormat();
        format.setCurrency(Currency.getInstance(Locale.CHINA));
        format.setGroupingUsed(groupingUsed);
        format.setMaximumFractionDigits(maximumFractionDigits);
        return format.format(cent / 100.00);
    }

    public static String formatWYuan(Long cent) {
        if (cent == null) {
            return null;
        }

        DecimalFormat format = new DecimalFormat();
        format.setCurrency(Currency.getInstance(Locale.CHINA));
        format.setGroupingUsed(false);
        return format.format(cent / 1000000.00);
    }


    static String hanDigiStr[] = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    static String hanDiviStr[] = new String[]{"", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟"};

    static String positiveIntegerToHanStr(String price) {
        String RMBStr = "";
        boolean lastzero = false;
        boolean hasvalue = false;
        int len, n;
        len = price.length();
        if (len > 15) {
            return "数值过大!";
        }
        for (int i = len - 1; i >= 0; i--) {
            if (price.charAt(len - i - 1) == ' ') {
                continue;
            }
            n = price.charAt(len - i - 1) - '0';
            if (n < 0 || n > 9) {
                return "输入含非数字字符!";
            }
            if (n != 0) {
                if (lastzero) {
                    RMBStr += hanDigiStr[0];
                }

                if (!(n == 1 && (i % 4) == 1 && (lastzero || i == len - 1))) {
                    if (!(n == 1 && (i % 4) == 1 && i == len - 1)) {
                        RMBStr += hanDigiStr[n];
                    }
                }
                RMBStr += hanDiviStr[i];
                hasvalue = true;
            } else {
                if ((i % 8) == 0 || ((i % 8) == 4 && hasvalue)) {
                    RMBStr += hanDiviStr[i];
                }
            }
            if (i % 8 == 0) {
                hasvalue = false;
            }
            lastzero = (n == 0) && (i % 4 != 0);
        }

        if (RMBStr.length() == 0) return hanDigiStr[0]; // 输入空字符或"0"，返回"零"
        return RMBStr;
    }

    /**
     * 1200 -->拾贰元整
     * * @param cent
     *
     * @return
     */
    public static String toRMB(long cent) {
        String SignStr = "";
        String TailStr = "";
        long fraction, integer;
        int jiao, fen;
        double val = cent / 100.00;
        if (val < 0) {
            val = -val;
            SignStr = "负";
        }
        if (val > 99999999999999.999 || val < -99999999999999.999) return "数值位数过大!";
        long temp = Math.round(val * 100);
        integer = temp / 100;
        fraction = temp % 100;
        jiao = (int) fraction / 10;
        fen = (int) fraction % 10;
        if (jiao == 0 && fen == 0) {
            TailStr = "整";
        } else {
            TailStr = hanDigiStr[jiao];
            if (jiao != 0) {
                TailStr += "角";
            }
            if (integer == 0 && jiao == 0) {
                TailStr = "";
            }
            if (fen != 0) {
                TailStr += hanDigiStr[fen] + "分";
            }
        }

        return SignStr + positiveIntegerToHanStr(String.valueOf(integer)) + "元" + TailStr;
    }


}
